<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Coupon extends CI_Model {

  /*
   * Coupon - the following class contains all the neccessary coupon operations.
   *
   * 1. Coupon operations
   * +-- useCoupon($code) - activate coupon. This operations must be performed only by partner.
   * +-- returnCoupon($code) - In order if user returns coupon. Must be performed by managers of deal.
   *
   * 2. Coupon getters
   * +-- useCoupon($code) - activate coupon. This operations must be performed only by partner.
   */


// Table Name
  var $table_name = 'coupons';

// -------------------------------------------------------------------------

// Coupon generation - Begin
  function generateCoupon($user_id, $prod_id, $deal_id, $partner_id, $product_name, $user_name, $cost, $enddate, $commission) {
    $b = false;
    $code = random_string('numeric', 10);

    while(!$b) {
      $code = random_string('numeric', 10);
      $this->db->where('code', $code);
      $res = $this->db->get($this->table_name);
      if($res->num_rows() == 0)
        $b = true;
    }

    $data = array('user_id'=>$user_id, 'prod_id'=>$prod_id, 'deal_id'=>$deal_id,
      'partner_id'=>$partner_id, 'prod_name'=>$product_name, 'user_name'=>$user_name, 
      'cost'=>$cost, 'enddate'=>$enddate, 'status' => 0, 'code' => $code, 'commission' => $commission
    );

    $this->db->insert($this->table_name, $data);
    return $this->db->insert_id();
  }

// Coupon generation - End


// Set coupon opened once

  function setPrinted($id) {
    $this->db->where('id',$id);
    $this->db->set(array('printed' => 1));
    $this->db->update($this->table_name);
  }

// -------------------------------------------------------------------------

// Coupon Operations - Begin

  function useCoupon($code, $partner_id) {
    $this->db->where('code',$code);
    $this->db->where('partner_id',$partner_id);
    $this->db->set(array('status' => 1));
		$this->db->update($this->table_name);
  }

  function returnCoupon($code) {
    $this->db->where('code',$code);
    $this->db->set(array('status' => 2));
    $this->db->update($this->table_name);
  }

// Coupon Operations - End

// -------------------------------------------------------------------------
  
// Coupon Getters - Begin

  //By User ID
  function getCouponByUserId($id){
    $this->db->where('user_id', $id);
    $res = $this->db->get($this->table_name);
    return $res->result();
  }

  //By Deal ID
  function getCouponByDealId($id){
    $this->db->where('deal_id', $id);
    $res = $this->db->get($this->table_name);
    return $res->result();
  }

  //By Product ID
  function getCouponByProductId($id){
    $this->db->where('prod_id', $id);
    $res = $this->db->get($this->table_name);
    return $res->result();
  }

  //By Partner ID
  function getCouponByPartnerId($id){
    $this->db->where('partner_id', $id);
    $res = $this->db->get($this->table_name);
    return $res->result();
  }

// Coupon Getters - End

// -------------------------------------------------------------------------

// Total money getters - Begin

  //By Product ID
  function getMoneyByProductId($id){
    $this->db->where('prod_id', $id);
    $res = $this->db->get($this->table_name);
    $rows = $res->result();
    $total = 0;
    foreach ($rows as $row) {
      $total += $row->cost;
    }
    return $total;
  }

  //By Deal ID
  function getMoneyByDealId($id){
    $this->db->where('deal_id', $id);
    $res = $this->db->get($this->table_name);
    $rows = $res->result();
    $total = 0;
    foreach ($rows as $row) {
      $total += $row->cost;
    }
    return $total;
  }

  //By Partner ID
  function getMoneyByPartnerId($id){
    $this->db->where('partner_id', $id);
    $res = $this->db->get($this->table_name);
    $rows = $res->result();
    $total = 0;
    foreach ($rows as $row) {
      $total += $row->cost;
    }
    return $total;
  }


  function getActiveMoneyByDealId($id){
   $this->db->where('deal_id', $id);
   $res = $this->db->get($this->table_name);
   $rows = $res->result();
   $total = 0;
   foreach ($rows as $row) {
     if($row->status == 1) $total += $row->cost;
   }
   return $total;
  }

  function getCountByDealId($id){
   $this->db->where('deal_id', $id);
   $res = $this->db->get($this->table_name);
   $rows = count($res->result());
   return $rows;
  }

  function getActiveCountByDealId($id){
   $this->db->where('deal_id', $id);
   $this->db->where('status', 1);
   $res = $this->db->get($this->table_name);
   $rows = count($res->result());
   return $rows;
 }  

// Total money getters - End

// -------------------------------------------------------------------------

// Commission money getters - Begin

  //By Product ID
  function getCommissionMoneyByProductId($id){
    $this->db->where('prod_id', $id);
    $this->db->where('status', 1);
    $res = $this->db->get($this->table_name);
    $rows = $res->result();
    $total = 0;
    foreach ($rows as $row) {
      $total += $row->cost / 100 * $row->commission;
    }
    return $total;
  }

  //By Deal ID
  function getCommissionMoneyByDealId($id){
    $this->db->where('deal_id', $id);
    $this->db->where('status', 1);
    $res = $this->db->get($this->table_name);
    $rows = $res->result();
    $total = 0;
    foreach ($rows as $row) {
      $total += $row->cost / 100 * $row->commission;
    }
    return $total;
  }

  //By Partner ID
  function getCommissionMoneyByPartnerId($id){
    $this->db->where('partner_id', $id);
    $this->db->where('status', 1);
    $res = $this->db->get($this->table_name);
    $rows = $res->result();
    $total = 0;
    foreach ($rows as $row) {
      $total += $row->cost / 100 * $row->commission;
    }
    return $total;
  }

// Commission money getters - End

// -------------------------------------------------------------------------

// Partner money getters - begin

  //By Product ID
  function getPartnerMoneyByProductId($id){
    $this->db->where('prod_id', $id);
    $this->db->where('status', 1);
    $res = $this->db->get($this->table_name);
    $rows = $res->result();
    $total = 0;
    foreach ($rows as $row) {
      $total += $row->cost / 100 * (100 - $row->commission);
    }
    return $total;
  }

  //By Deal ID
  function getPartnerMoneyByDealId($id){
    $this->db->where('deal_id', $id);
    $this->db->where('status', 1);
    $res = $this->db->get($this->table_name);
    $rows = $res->result();
    $total = 0;
    foreach ($rows as $row) {
      $total += $row->cost / 100 * (100 - $row->commission);
    }
    return $total;
  }

  //By Partner ID
  function getPartnerMoneyByPartnerId($id){
    $this->db->where('partner_id', $id);
    $this->db->where('status', 1);
    $res = $this->db->get($this->table_name);
    $rows = $res->result();
    $total = 0;
    foreach ($rows as $row) {
      $total += $row->cost / 100 * (100 - $row->commission);
    }
    return $total;
  }

// Partner money getters - End

// -------------------------------------------------------------------------

// Find coupon by code

  function find($code, $id) {
    return $this->db->query("SELECT * FROM ".$this->table_name." WHERE partner_id = ".$id." AND code LIKE '%".$code."%'")->result();
  }

  function getCouponById($id) {
    $this->db->where('id', $id);
    $res = $this->db->get($this->table_name);
    if($res->num_rows() > 0)
      return $res->result();
    else
      return false;
  }
}